using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Cards
{
    /// <summary>
    /// This is a game component that spiders through rows and looks for rows of stuff.
    /// </summary>
    public class Spider
    {
        //min anx max for oth the x and y
        const int MIN = 0;
        const int MAX = 10;

        static int[] totalChips = new int[2];


        public Spider()
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
        }


        public void AppendToInt(int[] a, int value)
        {
            for (int i = 0; i <= 5; i++)
            {
                if (a[i] == 9999)
                {
                    a[i] = value;
                    return;
                }
            } return;
        }


        public int ckUp(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);

            totalChips[(plrNum-1)] = 0;
            //check up
            for (int i = 1; i < 5; i++)
            {
                if (pos.Y - i > MIN)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc - (10 * i)] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc - (10 * i)])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc - (10 * i)]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                } else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            if (list.Count < 5)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckUp

        public int ckUp(int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);
            int returned = 0;

            totalChips[(plrNum - 1)] = 0;
            //check up
            for (int i = 1; i < 5; i++)
            {
                if (pos.Y - i > MIN)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc - (10 * i)] == (Board.chipState)plrNum)
                    {
                        returned++;
                    }
                    else break;
                }
                else break;
            }
            return returned;
        }//end ckUp

        
        public int ckDn(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);


            //Check Down
            for (int i = 1; i < 5; i++)
            {
                if (pos.Y + i < MAX)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc + (10 * i)] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc + (10 * i)])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc + (10 * i)]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                }
                else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            //if (list.Count < 5)
            //{
            //    list.Clear();
            //}
            //else if (list.Count == 5)
            //{
            //    //do nothing
            //}
            //else if (list.Count >= 6 && list.Count <= 9)
            //{
            //    EndGame.completeRows[(plrNum - 1)] = 1;
            //    for (int i = 5; i <= list.Count - 1; i++)
            //    {
            //        list.RemoveAt(i);
            //    }
            //}
            //else if (list.Count >= 10)
            //{
            //    EndGame.completeRows[(plrNum - 1)] = 2;
            //    //somebody WON!
            //}

            if (list.Count != 5 || list.Count != 10)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckDn

        public int ckL(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);

            totalChips[(plrNum - 1)] = 0;
            //check left
            for (int i = 1; i <= 5; i++)
            {
                if (pos.X - i > MIN)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc - i] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc - i])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc - i]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                }
                else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            if (list.Count < 5)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckL

        public int ckR(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);

            totalChips[(plrNum - 1)] = 0;
            //check right
            for (int i = 1; i < 5; i++)
            {
                if (pos.X + i < MAX)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc + i] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc + i])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc + i]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                }
                else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            if (list.Count < 5)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckR

        public int ckTL(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);

            totalChips[(plrNum - 1)] = 0;
            //check up
            for (int i = 1; i <= 5; i++)
            {
                if (pos.Y - i > MIN && pos.X - i >= MIN)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc - (10*i) - i] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc - (10*i) - i])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc - (10*i) - i]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                }
                else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            if (list.Count < 5)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckTL

        public int ckBL(List<int> list, int loc, int plrNum)
        {
            Vector2 pos = new Vector2();
            pos = Board.FindCoordinate10(loc);

            totalChips[(plrNum - 1)] = 0;
            //check up
            for (int i = 1; i < 5; i++)
            {
                if (pos.Y + i <= MAX && pos.X - i >= MIN)// && loc - (10 * i) > 0
                {
                    if (Board.chips[loc + (10 * i) - i] == (Board.chipState)plrNum /*&& !list.Contains(Game1.boardDeck.deck[loc + (10 * i) - i])*/)
                    {
                        if (list.Count == 0)
                        {
                            list.Add(Game1.boardDeck.deck[loc]);
                            totalChips[(plrNum - 1)]++;
                        }

                        list.Add(Game1.boardDeck.deck[loc + (10 * i) - i]);
                        totalChips[(plrNum - 1)]++;
                    }else break;
                }
                else break;
            }
            //dont do this for up, save it for down because we want to keep our list we've built until
            //we finish a full cardinal direction
            if (list.Count < 5)
            {
                list.Clear();
            }
            return totalChips[(plrNum - 1)];
        }//end ckBL

    }
}